package com.ziven.common.widget.value;

import android.view.View;

import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;

import com.ziven.common.widget.WidgetUtils;
import com.ziven.easygo.util.LogHelper;
import com.ziven.easygo.util.ResourceUtils;
import com.ziven.easygo.util.ViewHelper;

/**
 * @author Ziven
 */
public final class WidthHeight extends MixValue {

    private static final String TAG = "WidthHeight";
    private static final int SIZE = 2;
    private static final int NONE = -101;

    public static WidthHeight values(@NonNull Value... values) {
        return new WidthHeight(values);
    }

    public static WidthHeight dimenId(@DimenRes int dimenId) {
        return size(ResourceUtils.getDimen(dimenId));
    }

    public static WidthHeight dimenId(@DimenRes int widthDimenId, @DimenRes int heightDimenId) {
        return values(Width.dimenId(widthDimenId), Height.dimenId(heightDimenId));
    }

    public static WidthHeight size(@NonNull String size) {
        return size(WidgetUtils.dimenInt(size));
    }

    public static WidthHeight size(@NonNull String width, @NonNull String height) {
        return values(Width.size(width), Height.size(height));
    }

    public static WidthHeight size(int size) {
        return size(size, size);
    }

    public static WidthHeight size(int width, int height) {
        return values(Width.size(width), Height.size(height));
    }

    private WidthHeight(@NonNull Value... values) {
        super(values);
    }

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper, @NonNull Value[] values) {
        LogHelper.log(TAG, "WidthHeight.size:" + values.length);
        if(values.length == SIZE) {
            int width = getWidth(values);
            int height = getHeight(values);
            LogHelper.log(TAG, "width:" + width, " height:" + height);
            if(width != NONE && height != NONE) {
                helper.setWidthAndHeight(width, height);
            }
        }
    }

    private int getWidth(@NonNull Value[] values) {
        Value value = values[0] instanceof Width
                ? values[0]
                : values[1] instanceof Width
                ? values[1] : null;
        return value != null && value.value instanceof Integer
                ? (int) value.value : NONE;
    }

    private int getHeight(@NonNull Value[] values) {
        Value value = values[0] instanceof Height
                ? values[0]
                : values[1] instanceof Height
                ? values[1] : null;
        return value != null && value.value instanceof Integer
                ? (int) value.value : NONE;
    }
}
